package net.jueb.frameWorkDemo.mvc4mod.bean;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.util.Assert;

/**
 * 数据源上下文锁定
 * @author juebanlin
 */
public class DataSourceContextHolder extends AbstractRoutingDataSource {
    private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
    
    /**
     * 设置当前数据源
     * @param dataSource
     */
    public static void setDataSource(String dataSource) {
    	Assert.notNull(dataSource, "dataSource cannot be null"); 
        dataSourceKey.set(dataSource.trim());
    }
    
    /**
     * 获取当前数据源
     * @return
     */
    public static String getDataSource() {
        return dataSourceKey.get();
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return dataSourceKey.get();
    }
}